{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.ensemble import StackingRegressor, RandomForestRegressor\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.svm import SVR\n",
    "from sklearn.tree import DecisionTreeRegressor\n",
    "from sklearn.model_selection import train_test_split, cross_val_score\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.decomposition import PCA\n",
    "from sklearn.metrics import r2_score\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>MSSubClass</th>\n",
       "      <th>MSZoning</th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>Street</th>\n",
       "      <th>Alley</th>\n",
       "      <th>LotShape</th>\n",
       "      <th>LandContour</th>\n",
       "      <th>Utilities</th>\n",
       "      <th>LotConfig</th>\n",
       "      <th>...</th>\n",
       "      <th>PoolArea</th>\n",
       "      <th>PoolQC</th>\n",
       "      <th>Fence</th>\n",
       "      <th>MiscFeature</th>\n",
       "      <th>MiscVal</th>\n",
       "      <th>MoSold</th>\n",
       "      <th>YrSold</th>\n",
       "      <th>SaleType</th>\n",
       "      <th>SaleCondition</th>\n",
       "      <th>SalePrice</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>65.0</td>\n",
       "      <td>8450</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>208500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>20</td>\n",
       "      <td>RL</td>\n",
       "      <td>80.0</td>\n",
       "      <td>9600</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>2007</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>181500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>68.0</td>\n",
       "      <td>11250</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>223500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>70</td>\n",
       "      <td>RL</td>\n",
       "      <td>60.0</td>\n",
       "      <td>9550</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Corner</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2006</td>\n",
       "      <td>WD</td>\n",
       "      <td>Abnorml</td>\n",
       "      <td>140000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>84.0</td>\n",
       "      <td>14260</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>IR1</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>FR2</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>250000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1455</th>\n",
       "      <td>60</td>\n",
       "      <td>RL</td>\n",
       "      <td>62.0</td>\n",
       "      <td>7917</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>2007</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>175000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1456</th>\n",
       "      <td>20</td>\n",
       "      <td>RL</td>\n",
       "      <td>85.0</td>\n",
       "      <td>13175</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>MnPrv</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>210000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1457</th>\n",
       "      <td>70</td>\n",
       "      <td>RL</td>\n",
       "      <td>66.0</td>\n",
       "      <td>9042</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>GdPrv</td>\n",
       "      <td>Shed</td>\n",
       "      <td>2500</td>\n",
       "      <td>5</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>266500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1458</th>\n",
       "      <td>20</td>\n",
       "      <td>RL</td>\n",
       "      <td>68.0</td>\n",
       "      <td>9717</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>2010</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>142125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1459</th>\n",
       "      <td>20</td>\n",
       "      <td>RL</td>\n",
       "      <td>75.0</td>\n",
       "      <td>9937</td>\n",
       "      <td>Pave</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Reg</td>\n",
       "      <td>Lvl</td>\n",
       "      <td>AllPub</td>\n",
       "      <td>Inside</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>2008</td>\n",
       "      <td>WD</td>\n",
       "      <td>Normal</td>\n",
       "      <td>147500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1460 rows × 80 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      MSSubClass MSZoning  LotFrontage  LotArea Street Alley LotShape  \\\n",
       "0             60       RL         65.0     8450   Pave   NaN      Reg   \n",
       "1             20       RL         80.0     9600   Pave   NaN      Reg   \n",
       "2             60       RL         68.0    11250   Pave   NaN      IR1   \n",
       "3             70       RL         60.0     9550   Pave   NaN      IR1   \n",
       "4             60       RL         84.0    14260   Pave   NaN      IR1   \n",
       "...          ...      ...          ...      ...    ...   ...      ...   \n",
       "1455          60       RL         62.0     7917   Pave   NaN      Reg   \n",
       "1456          20       RL         85.0    13175   Pave   NaN      Reg   \n",
       "1457          70       RL         66.0     9042   Pave   NaN      Reg   \n",
       "1458          20       RL         68.0     9717   Pave   NaN      Reg   \n",
       "1459          20       RL         75.0     9937   Pave   NaN      Reg   \n",
       "\n",
       "     LandContour Utilities LotConfig  ... PoolArea PoolQC  Fence MiscFeature  \\\n",
       "0            Lvl    AllPub    Inside  ...        0    NaN    NaN         NaN   \n",
       "1            Lvl    AllPub       FR2  ...        0    NaN    NaN         NaN   \n",
       "2            Lvl    AllPub    Inside  ...        0    NaN    NaN         NaN   \n",
       "3            Lvl    AllPub    Corner  ...        0    NaN    NaN         NaN   \n",
       "4            Lvl    AllPub       FR2  ...        0    NaN    NaN         NaN   \n",
       "...          ...       ...       ...  ...      ...    ...    ...         ...   \n",
       "1455         Lvl    AllPub    Inside  ...        0    NaN    NaN         NaN   \n",
       "1456         Lvl    AllPub    Inside  ...        0    NaN  MnPrv         NaN   \n",
       "1457         Lvl    AllPub    Inside  ...        0    NaN  GdPrv        Shed   \n",
       "1458         Lvl    AllPub    Inside  ...        0    NaN    NaN         NaN   \n",
       "1459         Lvl    AllPub    Inside  ...        0    NaN    NaN         NaN   \n",
       "\n",
       "     MiscVal MoSold  YrSold  SaleType  SaleCondition  SalePrice  \n",
       "0          0      2    2008        WD         Normal     208500  \n",
       "1          0      5    2007        WD         Normal     181500  \n",
       "2          0      9    2008        WD         Normal     223500  \n",
       "3          0      2    2006        WD        Abnorml     140000  \n",
       "4          0     12    2008        WD         Normal     250000  \n",
       "...      ...    ...     ...       ...            ...        ...  \n",
       "1455       0      8    2007        WD         Normal     175000  \n",
       "1456       0      2    2010        WD         Normal     210000  \n",
       "1457    2500      5    2010        WD         Normal     266500  \n",
       "1458       0      4    2010        WD         Normal     142125  \n",
       "1459       0      6    2008        WD         Normal     147500  \n",
       "\n",
       "[1460 rows x 80 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_house = pd.read_csv('train.csv')\n",
    "df_house.drop('Id', axis=1, inplace=True)\n",
    "df_house"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Процент незаполненных значений в признаках:\n",
      "\n",
      "MSSubClass - 0.0%\n",
      "MSZoning - 0.0%\n",
      "LotFrontage - 17.74%\n",
      "LotArea - 0.0%\n",
      "Street - 0.0%\n",
      "Alley - 93.767%\n",
      "LotShape - 0.0%\n",
      "LandContour - 0.0%\n",
      "Utilities - 0.0%\n",
      "LotConfig - 0.0%\n",
      "LandSlope - 0.0%\n",
      "Neighborhood - 0.0%\n",
      "Condition1 - 0.0%\n",
      "Condition2 - 0.0%\n",
      "BldgType - 0.0%\n",
      "HouseStyle - 0.0%\n",
      "OverallQual - 0.0%\n",
      "OverallCond - 0.0%\n",
      "YearBuilt - 0.0%\n",
      "YearRemodAdd - 0.0%\n",
      "RoofStyle - 0.0%\n",
      "RoofMatl - 0.0%\n",
      "Exterior1st - 0.0%\n",
      "Exterior2nd - 0.0%\n",
      "MasVnrType - 0.548%\n",
      "MasVnrArea - 0.548%\n",
      "ExterQual - 0.0%\n",
      "ExterCond - 0.0%\n",
      "Foundation - 0.0%\n",
      "BsmtQual - 2.534%\n",
      "BsmtCond - 2.534%\n",
      "BsmtExposure - 2.603%\n",
      "BsmtFinType1 - 2.534%\n",
      "BsmtFinSF1 - 0.0%\n",
      "BsmtFinType2 - 2.603%\n",
      "BsmtFinSF2 - 0.0%\n",
      "BsmtUnfSF - 0.0%\n",
      "TotalBsmtSF - 0.0%\n",
      "Heating - 0.0%\n",
      "HeatingQC - 0.0%\n",
      "CentralAir - 0.0%\n",
      "Electrical - 0.068%\n",
      "1stFlrSF - 0.0%\n",
      "2ndFlrSF - 0.0%\n",
      "LowQualFinSF - 0.0%\n",
      "GrLivArea - 0.0%\n",
      "BsmtFullBath - 0.0%\n",
      "BsmtHalfBath - 0.0%\n",
      "FullBath - 0.0%\n",
      "HalfBath - 0.0%\n",
      "BedroomAbvGr - 0.0%\n",
      "KitchenAbvGr - 0.0%\n",
      "KitchenQual - 0.0%\n",
      "TotRmsAbvGrd - 0.0%\n",
      "Functional - 0.0%\n",
      "Fireplaces - 0.0%\n",
      "FireplaceQu - 47.26%\n",
      "GarageType - 5.548%\n",
      "GarageYrBlt - 5.548%\n",
      "GarageFinish - 5.548%\n",
      "GarageCars - 0.0%\n",
      "GarageArea - 0.0%\n",
      "GarageQual - 5.548%\n",
      "GarageCond - 5.548%\n",
      "PavedDrive - 0.0%\n",
      "WoodDeckSF - 0.0%\n",
      "OpenPorchSF - 0.0%\n",
      "EnclosedPorch - 0.0%\n",
      "3SsnPorch - 0.0%\n",
      "ScreenPorch - 0.0%\n",
      "PoolArea - 0.0%\n",
      "PoolQC - 99.521%\n",
      "Fence - 80.753%\n",
      "MiscFeature - 96.301%\n",
      "MiscVal - 0.0%\n",
      "MoSold - 0.0%\n",
      "YrSold - 0.0%\n",
      "SaleType - 0.0%\n",
      "SaleCondition - 0.0%\n",
      "SalePrice - 0.0%\n"
     ]
    }
   ],
   "source": [
    "print('Процент незаполненных значений в признаках:\\n')\n",
    "for column_name in df_house.columns:\n",
    "    col_stat = round(df_house[column_name].isna().mean() * 100, 3)\n",
    "    print(f'{column_name} - {col_stat}%')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. MSSubClass: Тип жилья для продажи. Категориальное номинальное значение. Требуется One Hot Encoding. Пропусков нет\n",
    "\n",
    "2. MSZoning: Определяет общую классификацию зонирования продажи. Требуется One Hot Encoding.\n",
    "\n",
    "3. LotFrontage: Линейные метры улицы, соединенной с недвижимостью. Требуется заполнение пропущенных данных (17.74%). Есть выбросы, оставляем данные с LotFrontage <= 100. Пропуски удалим.\n",
    "\n",
    "4. LotArea: Размер лота в квадратных футах. Есть выборосы, оставляем данные с LotArea <= 15000\n",
    "\n",
    "5. Street: Тип подъездной дороги к объекту недвижимости. Категориальное номинальное значение. Требуется One Hot Encoding. \n",
    "\n",
    "6. Alley: Тип подъездной аллеи к собственности. Категориальное номинальное значение. Требуется One Hot Encoding. Здесь NaN обозначает отсутствие подъездной аллеи, необходимо его заменить на текстовое значение \"None\". Добавим новый признак наличия аллеи.\n",
    "\n",
    "7. LotShape: Общая форма собственности. Категориальное номинальное значение. Требуется One Hot Encoding. \n",
    "\n",
    "8. LandContour: Плоскостность участка. Категориальное номинальное значение. Требуется One Hot Encoding.\n",
    "\n",
    "9. Utilities: Тип доступных коммунальных услуг. Категориальное порядковое значение. Требуется LabelEncoder.\n",
    "\n",
    "10. LotConfig: Конфигурация лота. Категориальное номинальное значение. Требуется One Hot Encoding.\n",
    "\n",
    "11. LandSlope: склон собственности. Ксатегориальное порядковое значение. Требуется LabelEncoder\n",
    "\n",
    "12. Neighborhood: Район. Категориальное номинальное значение. Требуется One Hot Encoding.\n",
    "\n",
    "13. Condition1: Proximity to various conditions. Категориальное номинальное значение. Требуется One Hot Encoding\n",
    "\n",
    "14. Condition2: Proximity to various conditions (if more than one is present). Категориальное номинальное значение. Требуется One Hot Encoding и конкатенация с Condition1\n",
    "\n",
    "15. BldgType: Тип жилого помещения. Категориальное номинальное значение. Требуется One Hot Encoding\n",
    "\n",
    "16. HouseStyle: Стиль дома. Категориальное номинальное значение. Требуется One Hot Encoding\n",
    "\n",
    "17. OverallQual (Общее качество): Оценивает общий материал и отделку дома\n",
    "\n",
    "18. OverallCond (Общее состояние): Оценивает общее состояние дома\n",
    "\n",
    "19. YearBuilt: Дата строительства дома\n",
    "\n",
    "20. YearRemodAdd: Дата реконструкции (такая же, как дата строительства, если нет реконструкции или дополнений)\n",
    "\n",
    "21. RoofStyle: Тип крыши. Категориальное номинальное значение. Требуется One Hot Encoding\n",
    "\n",
    "22. RoofMatl: Материал крыши. Категориальное номинальное значение. Требуется One Hot Encoding\n",
    "\n",
    "23. Exterior1st: Наружное покрытие дома. Категориальное номинальное значение. Требуется One Hot Encoding\n",
    "\n",
    "24. Exterior2nd: Наружное покрытие дома (если более одного материала). Категориальное номинальное значение. Требуется One Hot Encoding и конкатенация с Exterior1st\n",
    "\n",
    "25. MasVnrType: Тип облицовки каменной кладки. Категориальное номинальное значение. Требуется One Hot Encoding. NaN обозначает отсутствие облицовки, требуется замена на текстовый None. Создадим новый признак наличия облицовки каменной кладки.\n",
    "\n",
    "26. MasVnrArea: Площадь облицовки каменной кладки в квадратных футах. Пропуски заполняем нулевым значением, так как это означает, что в MasVnrType отсутствует облицовка.\n",
    "\n",
    "27. ExterQual: Оценивает качество материала на внешней стороне. Категориальное порядковое значение, требуется LabelEncoder\n",
    "\n",
    "28. ExtraCond: Оценивает текущее состояние материала на внешней стороне. Категориальное порядковое значение, требуется LabelEncoder\n",
    "\n",
    "29. Foundation: Тип фундамента. Категориальное номинальное значение. Требуется One Hot Encoding.\n",
    "\n",
    "30. BsmtQual: Оценивает высоту подвала. Категориальное порядковое значение, требуется LabelEncoder. Также NaN требуется заменить на текстовое \"None\", это означает отсутствие подвала. Создадим новый признак наличия подвала.\n",
    "\n",
    "31. BsmtCond: Оценивает общее состояние подвала. Категориальное порядковое значение, требуется LabelEncoder. Также NaN требуется заменить на текстовое \"None\", это означает отсутствие подвала.\n",
    "\n",
    "32. BsmtExposure: Экспозиция подвала. Категориальное порядковое значение, требуется LabelEncoder. Также NaN требуется заменить на текстовое \"None\", это означает отсутствие подвала.\n",
    "\n",
    "33. BsmtFinType1: Оценка готовой площади подвала. Категориальное порядковое значение, требуется LabelEncoder. Также NaN требуется заменить на текстовое \"None\", это означает отсутствие подвала.\n",
    "\n",
    "34. BsmtFinSF1: готовые квадратные футы подвала типа 1\n",
    "\n",
    "35. BsmtFinType2: Оценка готовой площади подвала (если выбрано несколько). Категориальное порядковое значение, требуется LabelEncoder. Также NaN требуется заменить на текстовое \"None\", это означает отсутствие подвала.\n",
    "\n",
    "36. BsmtFinSF2:готовые квадратные футы подвала типа 1\n",
    "\n",
    "37. BsmtUnfSF: незаконченное кв. м площадь подвала\n",
    "\n",
    "38. TotalBsmtSF: Общая площадь подвала в квадратных футах\n",
    "\n",
    "39. Heating: Тип отопления. Категориальное номинальное значение. Требуется One Hot Encoding\n",
    "\n",
    "40. HeatingQC: Качество и состояние отопления. Категориальное порядковое значение, требуется LabelEncoder.\n",
    "\n",
    "41. CentralAir: Центральное кондиционирование. Флаг, требуется LabelEncoder.\n",
    "\n",
    "42. Electrical: Электрическая система. Категориальное номинальное значение. Требуется One Hot Encoding. Требуется заполнение пропусков (0.094%). Заполним модой.\n",
    "\n",
    "43. 1stFlrSF: Первый этаж квадратных футов\n",
    " \n",
    "44. 2ndFlrSF: Второй этаж квадратных футов\n",
    "\n",
    "45. LowQualFinSF: Низкокачественные готовые квадратные футы (все этажи)\n",
    "\n",
    "46. GrLivArea: Above grade (ground) living area square feet\n",
    "\n",
    "47. BsmtFullBath: Basement full bathrooms\n",
    "\n",
    "48. BsmtHalfBath: Basement half bathrooms\n",
    "\n",
    "49. FullBath: Full bathrooms above grade\n",
    "\n",
    "50. HalfBath: Half baths above grade\n",
    "\n",
    "51. BedroomAbvGr: Bedrooms above grade (does NOT include basement bedrooms)\n",
    "\n",
    "52. KitchenAbvGr: Kitchens above grade\n",
    "\n",
    "53. KitchenQual: Качество кухни. Категориальное номинальное значение. Требуется One Hot Encoding. \n",
    "\n",
    "54. TotRmsAbvGrd: Общее количество номеров высокого класса (не включает ванные комнаты)\n",
    "\n",
    "55. Functional: Домашняя функциональность. Категориальное номинальное значение. Требуется One Hot Encoding. \n",
    "\n",
    "56. Fireplaces: Количество каминов\n",
    "\n",
    "57. FireplaceQu: Качество камина. Категориальное номинальное значение. Требуется One Hot Encoding. Требуется заполнение пропусков (49.925%). Отсутствующие значения заполняем текстовым None, это означает отсутствие камина. Создадим новый признак наличия камина.\n",
    "\n",
    "58. GarageType: Расположение гаража. Категориальное номинальное значение. Требуется One Hot Encoding. Требуется заполнение пропусков (5.904%). Отсутствующие значения заполняем текстовым None, это означает отсутствие гаража. Так же создадим дополнительный признак: 0 - гаража нет, 1 - гараж есть\n",
    "\n",
    "59. GarageYrBlt: Год постройки гаража. Требуется заполнение пропусков (5.904%). Пропуски стоят тоолько при отсутствии гаража. Заполним их 0.\n",
    "\n",
    "60. GarageFinish: Внутренняя отделка гаража. Категориальное номинальное значение. Требуется One Hot Encoding. Отсутствующие значения заполняем текстовым None, это означает отсутствие гаража\n",
    "\n",
    "61. GarageCars: Размер гаража по вместимости автомобиля\n",
    "\n",
    "62. GarageArea: Размер гаража в квадратных футах\n",
    "\n",
    "63. GarageQual: Качество гаража. Категориальное порядковое значение, требуется LabelEncoder. Промущенные значение заполняем строкой 'None', это означает отсутствие гаража.\n",
    "\n",
    "64. GarageCond: Состояние гаража. Категориальное порядковое значение, требуется LabelEncoder. Промущенные значение заполняем строкой 'None', это означает отсутствие гаража.\n",
    "\n",
    "65. PavedDrive: Асфальтированная подъездная дорожка. Категориальное номинальное значение. Требуется One Hot Encoding.\n",
    "\n",
    "66. WoodDeckSF: Площадь деревянного настила в квадратных футах\n",
    "\n",
    "67. OpenPorchSF: Площадь открытой веранды в квадратных футах\n",
    "\n",
    "68. EnclosedPorch: Площадь крыльца в квадратных футах.\n",
    "\n",
    "69. 3SsnPorch: Площадь крыльца на три сезона в квадратных футах\n",
    "\n",
    "70. ScreenPorch: Площадь крыльца экрана в квадратных футах\n",
    "\n",
    "71. PoolArea: Площадь бассейна в квадратных футах. \n",
    "\n",
    "72. PoolQC: качество бассейна. Категориальное порядковое значение, требуется LabelEncoder. Требуется заполнение пропусков (99.521%). Пропущенные значение заполняем строкой 'None', это означает отсутствие бассейна. Так же создадим дополнительный признак: 0 - бассейна нет, 1 - бассейн есть\n",
    "\n",
    "73. Fence: Качество забора. Категориальное порядковое значение, требуется LabelEncoder. Требуется заполнение пропусков (80.753%). Пропущенные значение заполняем строкой 'None', это означает отсутствие забора. Так же создадим дополнительный признак: 0 - забора нет, 1 - забор есть\n",
    "\n",
    "74. MiscFeature: особенности, не вошедшие в другие категории. Категориальное номинальное значение. Требуется One Hot Encoding. Требуется заполнение пропусков (96.301%). Отсутствующие значения заполняем текстовым None, это означает отсутствие гаража. Так же создадим дополнительный признак: 0 - нет иных особенностей, 1 - есть иные особенности\n",
    "\n",
    "75. MiscVal: $Value of miscellaneous feature\n",
    "\n",
    "76. MoSold: месяц продажи.\n",
    "\n",
    "77. YrSold: год.продажи. Создадим новый признак с датой продажи, который объединит год и месяц. А признаки с отдельными значениями удалим.\n",
    "\n",
    "78. SaleType: тип продажи. Категориальное номинальное значение. Требуется One Hot Encoding.\n",
    "\n",
    "79. SaleCondition: Condition of sale. Категориальное номинальное значение. Требуется One Hot Encoding."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_house = df_house[df_house.LotFrontage <= 100]\n",
    "df_house = df_house[df_house.LotArea < 15000]\n",
    "\n",
    "# Создаем новый признак наличия подъездной аллеи к собственности\n",
    "df_house.loc[:, 'Alley_isExist'] = df_house.Alley.apply(lambda x: 0 if pd.isna(x) else 1)\n",
    "df_house.loc[df_house.Alley.isna(), 'Alley'] = 'None'\n",
    "\n",
    "mapping_Utilities = {'ELO': 1, 'NoSeWa': 2, 'NoSewr': 3, 'AllPub': 4}\n",
    "df_house.loc[:, 'Utilities'] = df_house.Utilities.map(mapping_Utilities)\n",
    "\n",
    "mapping_LandSlope = {'Gtl': 1, 'Mod': 2, 'Sev': 3}\n",
    "df_house.loc[:, 'LandSlope'] = df_house.LandSlope.map(mapping_LandSlope)\n",
    "\n",
    "# Создаем новый признак наличия облицовки каменной кладки\n",
    "df_house.loc[:, 'MasVnr_isExist'] = df_house.MasVnrType.apply(lambda x: 0 if pd.isna(x) else 1)\n",
    "df_house.loc[df_house.MasVnrType.isna(), 'MasVnrType'] = 'None'\n",
    "\n",
    "df_house.loc[:, 'Condition1+Condition2'] = df_house['Condition1'] + '+' + df_house['Condition2']\n",
    "df_house.loc[:, 'Exterior1st+Exterior2nd'] = df_house['Exterior1st'] + '+' + df_house['Exterior2nd']\n",
    "df_house.drop(['Condition1', 'Condition2', 'Exterior1st', 'Exterior2nd'], axis=1, inplace=True)\n",
    "df_house.loc[df_house.MasVnrArea.isna(), 'MasVnrArea'] = 0\n",
    "\n",
    "mapping_ExterQual = {'Po': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}\n",
    "df_house.ExterQual = df_house.ExterQual.map(mapping_ExterQual)\n",
    "\n",
    "mapping_ExterCond = {'Po': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}\n",
    "df_house.ExterCond = df_house.ExterCond.map(mapping_ExterCond)\n",
    "\n",
    "# Создаем новый признак наличия подвала\n",
    "df_house.loc[:, 'Bsmt_isExist'] = df_house.BsmtQual.apply(lambda x: 0 if pd.isna(x) else 1)\n",
    "mapping_BsmtQual = {'None': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}\n",
    "df_house.loc[df_house.BsmtQual.isna(), 'BsmtQual'] = 'None'\n",
    "df_house.loc[:, 'BsmtQual'] = df_house.BsmtQual.map(mapping_BsmtQual)\n",
    "\n",
    "mapping_BsmtCond = {'None': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}\n",
    "df_house.loc[df_house.BsmtCond.isna(), 'BsmtCond'] = 'None'\n",
    "df_house.loc[:, 'BsmtCond'] = df_house.BsmtCond.map(mapping_BsmtCond)\n",
    "\n",
    "df_house.loc[df_house.BsmtExposure.isna(), 'BsmtExposure'] = 'None'\n",
    "mapping_BsmtExposure = {'None': 0, 'No': 1, 'Mn': 2, 'Av': 3, 'Gd': 4}\n",
    "df_house.loc[:, 'BsmtExposure'] = df_house.BsmtExposure.map(mapping_BsmtExposure)\n",
    "\n",
    "df_house.loc[df_house.BsmtFinType1.isna(), 'BsmtFinType1'] = 'None'\n",
    "mapping_BsmtFinType1 = {'None': 0, 'Unf': 1, 'LwQ': 2, 'Rec': 3, 'BLQ': 4, 'ALQ': 5, 'GLQ': 6}\n",
    "df_house.loc[:, 'BsmtFinType1'] = df_house.BsmtFinType1.map(mapping_BsmtFinType1)\n",
    "\n",
    "df_house.loc[df_house.BsmtFinType2.isna(), 'BsmtFinType2'] = 'None'\n",
    "mapping_BsmtFinType2 = mapping_BsmtFinType1\n",
    "df_house.loc[:, 'BsmtFinType2'] = df_house.BsmtFinType2.map(mapping_BsmtFinType2)\n",
    "\n",
    "mapping_HeatingQC = {'Po': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}\n",
    "df_house.loc[:, 'HeatingQC'] = df_house.HeatingQC.map(mapping_HeatingQC)\n",
    "\n",
    "mapping_CentralAir = {'N': 0, 'Y': 1}\n",
    "df_house.loc[:, 'CentralAir'] = df_house.CentralAir.map(mapping_CentralAir)\n",
    "\n",
    "# Заполняем модой, SBrkr - самое частое значение\n",
    "df_house.loc[df_house.Electrical.isna(), 'Electrical'] = 'SBrkr'\n",
    "\n",
    "mapping_KitchenQual = {'Po': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}\n",
    "df_house.loc[:, 'KitchenQual'] = df_house.KitchenQual.map(mapping_KitchenQual)\n",
    "\n",
    "# Создаем новый признак наличия гаража\n",
    "df_house.loc[:, 'Garage_isExist'] = df_house.GarageType.apply(lambda x: 0 if pd.isna(x) else 1)\n",
    "df_house.loc[df_house.GarageType.isna(), 'GarageType'] = 'None'\n",
    "\n",
    "df_house.loc[df_house.GarageYrBlt.isna(), 'GarageYrBlt'] = 0\n",
    "\n",
    "df_house.loc[df_house.GarageFinish.isna(), 'GarageFinish'] = 'None'\n",
    "\n",
    "df_house.loc[df_house.GarageQual.isna(), 'GarageQual'] = 'None'\n",
    "mapping_GarageQual = {'None': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}\n",
    "df_house.loc[:, 'GarageQual'] = df_house.GarageQual.map(mapping_GarageQual)\n",
    "\n",
    "# Создаем новый признак наличия камина\n",
    "df_house.loc[:, 'FireplaceQu_isExist'] = df_house.FireplaceQu.apply(lambda x: 0 if pd.isna(x) else 1)\n",
    "df_house.loc[df_house.FireplaceQu.isna(), 'FireplaceQu'] = 'None'\n",
    "mapping_FireplaceQu = {'None': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}\n",
    "df_house.loc[:, 'FireplaceQu'] = df_house.FireplaceQu.map(mapping_FireplaceQu)\n",
    "\n",
    "df_house.loc[df_house.GarageCond.isna(), 'GarageCond'] = 'None'\n",
    "mapping_GarageCond = {'None': 0, 'Po': 1, 'Fa': 2, 'TA': 3, 'Gd': 4, 'Ex': 5}\n",
    "df_house.loc[:, 'GarageCond'] = df_house.GarageCond.map(mapping_GarageCond)\n",
    "\n",
    "# Создаем новый признак наличия бассейна\n",
    "df_house.loc[:, 'Pool_isExist'] = df_house.PoolQC.apply(lambda x: 0 if pd.isna(x) else 1)\n",
    "df_house.loc[df_house.PoolQC.isna(), 'PoolQC'] = 'None'\n",
    "mapping_PoolQC = {'None': 0, 'Fa': 1, 'TA': 2, 'Gd': 3, 'Ex': 4}\n",
    "df_house.loc[:, 'PoolQC'] = df_house.PoolQC.map(mapping_PoolQC)\n",
    "\n",
    "# Создаем новый признак наличия забора\n",
    "df_house.loc[:, 'Fence_isExist'] = df_house.Fence.apply(lambda x: 0 if pd.isna(x) else 1)\n",
    "df_house.loc[df_house.Fence.isna(), 'Fence'] = 'None'\n",
    "mapping_Fence = {'None': 0, 'MnWw': 1, 'GdWo': 2, 'MnPrv': 3, 'GdPrv': 4}\n",
    "df_house.loc[:, 'Fence'] = df_house.Fence.map(mapping_Fence)\n",
    "\n",
    "# Создаем новый признак наличия иных особенностей\n",
    "df_house.loc[:, 'MiscFeature_isExist'] = df_house.MiscFeature.apply(lambda x: 0 if pd.isna(x) else 1)\n",
    "df_house.loc[df_house.MiscFeature.isna(), 'MiscFeature'] = 'None'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "one_hot_features = ['MSSubClass', 'MSZoning', 'Street', 'Alley', 'LotShape', 'LandContour', 'LotConfig',\n",
    "                   'Neighborhood', 'Condition1+Condition2', 'BldgType', 'HouseStyle', 'RoofStyle', \n",
    "                   'RoofMatl', 'Exterior1st+Exterior2nd', 'MasVnrType', 'Foundation', 'Heating',\n",
    "                   'Functional', 'GarageType', 'GarageFinish', 'PavedDrive', 'MiscFeature', 'SaleType',\n",
    "                   'SaleCondition', 'Electrical']\n",
    "df_house = pd.get_dummies(df_house, columns=one_hot_features)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Проанализируем на предмет выборосов основные численные признаки"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>LotFrontage</th>\n",
       "      <th>LotArea</th>\n",
       "      <th>MasVnrArea</th>\n",
       "      <th>BsmtFinSF1</th>\n",
       "      <th>BsmtFinSF2</th>\n",
       "      <th>BsmtUnfSF</th>\n",
       "      <th>TotalBsmtSF</th>\n",
       "      <th>1stFlrSF</th>\n",
       "      <th>2ndFlrSF</th>\n",
       "      <th>LowQualFinSF</th>\n",
       "      <th>LowQualFinSF</th>\n",
       "      <th>GrLivArea</th>\n",
       "      <th>GarageArea</th>\n",
       "      <th>WoodDeckSF</th>\n",
       "      <th>OpenPorchSF</th>\n",
       "      <th>EnclosedPorch</th>\n",
       "      <th>3SsnPorch</th>\n",
       "      <th>ScreenPorch</th>\n",
       "      <th>PoolArea</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "      <td>1067.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>65.663543</td>\n",
       "      <td>8707.999063</td>\n",
       "      <td>95.082474</td>\n",
       "      <td>406.343018</td>\n",
       "      <td>40.008435</td>\n",
       "      <td>572.681350</td>\n",
       "      <td>1019.032802</td>\n",
       "      <td>1120.339269</td>\n",
       "      <td>329.966261</td>\n",
       "      <td>5.247423</td>\n",
       "      <td>5.247423</td>\n",
       "      <td>1455.552952</td>\n",
       "      <td>455.505155</td>\n",
       "      <td>84.582943</td>\n",
       "      <td>42.475164</td>\n",
       "      <td>23.183693</td>\n",
       "      <td>3.208997</td>\n",
       "      <td>13.251172</td>\n",
       "      <td>1.147142</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>17.273167</td>\n",
       "      <td>2829.386345</td>\n",
       "      <td>170.170593</td>\n",
       "      <td>414.780252</td>\n",
       "      <td>145.311934</td>\n",
       "      <td>432.069512</td>\n",
       "      <td>395.852586</td>\n",
       "      <td>345.740456</td>\n",
       "      <td>413.469912</td>\n",
       "      <td>43.929893</td>\n",
       "      <td>43.929893</td>\n",
       "      <td>459.613019</td>\n",
       "      <td>211.890044</td>\n",
       "      <td>113.337677</td>\n",
       "      <td>62.610384</td>\n",
       "      <td>60.586703</td>\n",
       "      <td>29.693373</td>\n",
       "      <td>51.845027</td>\n",
       "      <td>26.529673</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>21.000000</td>\n",
       "      <td>1300.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>334.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>334.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>57.000000</td>\n",
       "      <td>7200.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>240.500000</td>\n",
       "      <td>776.000000</td>\n",
       "      <td>864.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1117.500000</td>\n",
       "      <td>300.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>66.000000</td>\n",
       "      <td>8993.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>349.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>495.000000</td>\n",
       "      <td>960.000000</td>\n",
       "      <td>1056.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1412.000000</td>\n",
       "      <td>472.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>20.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>78.000000</td>\n",
       "      <td>10557.000000</td>\n",
       "      <td>150.500000</td>\n",
       "      <td>670.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>810.500000</td>\n",
       "      <td>1247.500000</td>\n",
       "      <td>1327.500000</td>\n",
       "      <td>705.500000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>1717.500000</td>\n",
       "      <td>576.000000</td>\n",
       "      <td>160.000000</td>\n",
       "      <td>63.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>100.000000</td>\n",
       "      <td>14963.000000</td>\n",
       "      <td>1600.000000</td>\n",
       "      <td>2188.000000</td>\n",
       "      <td>1474.000000</td>\n",
       "      <td>2046.000000</td>\n",
       "      <td>3206.000000</td>\n",
       "      <td>2524.000000</td>\n",
       "      <td>1611.000000</td>\n",
       "      <td>515.000000</td>\n",
       "      <td>515.000000</td>\n",
       "      <td>3395.000000</td>\n",
       "      <td>1390.000000</td>\n",
       "      <td>736.000000</td>\n",
       "      <td>547.000000</td>\n",
       "      <td>386.000000</td>\n",
       "      <td>508.000000</td>\n",
       "      <td>480.000000</td>\n",
       "      <td>648.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       LotFrontage       LotArea   MasVnrArea   BsmtFinSF1   BsmtFinSF2  \\\n",
       "count  1067.000000   1067.000000  1067.000000  1067.000000  1067.000000   \n",
       "mean     65.663543   8707.999063    95.082474   406.343018    40.008435   \n",
       "std      17.273167   2829.386345   170.170593   414.780252   145.311934   \n",
       "min      21.000000   1300.000000     0.000000     0.000000     0.000000   \n",
       "25%      57.000000   7200.000000     0.000000     0.000000     0.000000   \n",
       "50%      66.000000   8993.000000     0.000000   349.000000     0.000000   \n",
       "75%      78.000000  10557.000000   150.500000   670.000000     0.000000   \n",
       "max     100.000000  14963.000000  1600.000000  2188.000000  1474.000000   \n",
       "\n",
       "         BsmtUnfSF  TotalBsmtSF     1stFlrSF     2ndFlrSF  LowQualFinSF  \\\n",
       "count  1067.000000  1067.000000  1067.000000  1067.000000   1067.000000   \n",
       "mean    572.681350  1019.032802  1120.339269   329.966261      5.247423   \n",
       "std     432.069512   395.852586   345.740456   413.469912     43.929893   \n",
       "min       0.000000     0.000000   334.000000     0.000000      0.000000   \n",
       "25%     240.500000   776.000000   864.000000     0.000000      0.000000   \n",
       "50%     495.000000   960.000000  1056.000000     0.000000      0.000000   \n",
       "75%     810.500000  1247.500000  1327.500000   705.500000      0.000000   \n",
       "max    2046.000000  3206.000000  2524.000000  1611.000000    515.000000   \n",
       "\n",
       "       LowQualFinSF    GrLivArea   GarageArea   WoodDeckSF  OpenPorchSF  \\\n",
       "count   1067.000000  1067.000000  1067.000000  1067.000000  1067.000000   \n",
       "mean       5.247423  1455.552952   455.505155    84.582943    42.475164   \n",
       "std       43.929893   459.613019   211.890044   113.337677    62.610384   \n",
       "min        0.000000   334.000000     0.000000     0.000000     0.000000   \n",
       "25%        0.000000  1117.500000   300.000000     0.000000     0.000000   \n",
       "50%        0.000000  1412.000000   472.000000     0.000000    20.000000   \n",
       "75%        0.000000  1717.500000   576.000000   160.000000    63.000000   \n",
       "max      515.000000  3395.000000  1390.000000   736.000000   547.000000   \n",
       "\n",
       "       EnclosedPorch    3SsnPorch  ScreenPorch     PoolArea  \n",
       "count    1067.000000  1067.000000  1067.000000  1067.000000  \n",
       "mean       23.183693     3.208997    13.251172     1.147142  \n",
       "std        60.586703    29.693373    51.845027    26.529673  \n",
       "min         0.000000     0.000000     0.000000     0.000000  \n",
       "25%         0.000000     0.000000     0.000000     0.000000  \n",
       "50%         0.000000     0.000000     0.000000     0.000000  \n",
       "75%         0.000000     0.000000     0.000000     0.000000  \n",
       "max       386.000000   508.000000   480.000000   648.000000  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_house[['LotFrontage', 'LotArea', 'MasVnrArea', 'BsmtFinSF1', 'BsmtFinSF2', 'BsmtUnfSF', \n",
    "          'TotalBsmtSF', '1stFlrSF', '2ndFlrSF', 'LowQualFinSF', 'LowQualFinSF', 'GrLivArea',\n",
    "          'GarageArea', 'WoodDeckSF', 'OpenPorchSF', 'EnclosedPorch', '3SsnPorch', 'ScreenPorch', \n",
    "          'PoolArea']].describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Явные выбросы были устранены. Признаки, которые имеют много нулевых значений, принято решений оставить как есть.\n",
    "\n",
    "Найдем самые значимые признаки с помощью случайного леса."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "RandomForestRegressor(n_estimators=1000)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(df_house[df_house.columns.drop('SalePrice')],\n",
    "                                                    df_house['SalePrice'],\n",
    "                                                    test_size=0.3,\n",
    "                                                    random_state=42)\n",
    "rand_forest = RandomForestRegressor(n_estimators=1000)\n",
    "rand_forest.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OverallQual: 0.6535289578384785\n",
      "GrLivArea: 0.07875768508703847\n",
      "BsmtFinSF1: 0.025415439687847506\n",
      "ExterQual: 0.02281137758972538\n",
      "TotalBsmtSF: 0.02109602359866852\n",
      "YearBuilt: 0.018584576173163246\n",
      "GarageCars: 0.01626903217770852\n",
      "GarageArea: 0.016231682001080043\n",
      "BsmtQual: 0.015263954733924438\n",
      "1stFlrSF: 0.012576746557934107\n",
      "LotArea: 0.00925913857561885\n",
      "YearRemodAdd: 0.007798338790001757\n",
      "LotFrontage: 0.0059753319095211645\n",
      "GarageYrBlt: 0.005267239138943357\n",
      "GarageType_Attchd: 0.005195290040652447\n",
      "2ndFlrSF: 0.005117765137869254\n",
      "BsmtUnfSF: 0.0043877686071328885\n",
      "OverallCond: 0.0041790185250555395\n",
      "WoodDeckSF: 0.004075923461238196\n",
      "KitchenQual: 0.003935946454142492\n",
      "OpenPorchSF: 0.0038787200967355146\n",
      "FireplaceQu: 0.003690104941366778\n",
      "TotRmsAbvGrd: 0.0030789738031615855\n",
      "MasVnrArea: 0.002786407164848996\n",
      "BsmtFinType1: 0.002737240594145092\n",
      "MoSold: 0.0027238811381462566\n",
      "FullBath: 0.002086917476660799\n",
      "MSZoning_RM: 0.0019220181924580928\n",
      "BsmtExposure: 0.001901026825544406\n",
      "CentralAir: 0.0018403014517953352\n",
      "SaleType_WD: 0.0017411649832836727\n",
      "YrSold: 0.001380135916750053\n",
      "BedroomAbvGr: 0.0013706057867933416\n",
      "GarageType_Detchd: 0.0013307124485376796\n",
      "HalfBath: 0.0012014802881266241\n",
      "GarageFinish_Unf: 0.0011932335427424212\n",
      "ExterCond: 0.0008413875736644856\n",
      "Fireplaces: 0.0008326718758899721\n",
      "GarageFinish_RFn: 0.0007688701426939063\n",
      "HeatingQC: 0.0007027784363100744\n",
      "BsmtCond: 0.0006674884453497517\n",
      "Exterior1st+Exterior2nd_VinylSd+VinylSd: 0.0006562668453799216\n",
      "FireplaceQu_isExist: 0.0006543961385047057\n",
      "MSSubClass_30: 0.000624307954870871\n",
      "LandContour_Lvl: 0.0006095139119913658\n",
      "GarageQual: 0.0006015638418271429\n",
      "SaleCondition_Family: 0.0005714744376269782\n",
      "SaleType_New: 0.0005600684166554968\n",
      "MSZoning_RL: 0.0005205445000455985\n",
      "SaleCondition_Partial: 0.0005192062383079125\n",
      "Exterior1st+Exterior2nd_MetalSd+MetalSd: 0.000518001353414774\n",
      "BsmtFullBath: 0.0005130866204183959\n",
      "SaleCondition_Normal: 0.000505523153291838\n",
      "MSSubClass_60: 0.00047359199315349124\n",
      "MasVnrType_Stone: 0.0004600947716574231\n",
      "MSSubClass_20: 0.0004366851106766821\n",
      "GarageFinish_Fin: 0.00043450222104580316\n",
      "RoofStyle_Gable: 0.00042976131288538527\n",
      "LandContour_HLS: 0.0004202560887913143\n",
      "GarageType_BuiltIn: 0.0004118323962551937\n",
      "KitchenAbvGr: 0.00040522733532507546\n",
      "EnclosedPorch: 0.00039042686629413547\n",
      "Neighborhood_Crawfor: 0.00038244400074581824\n",
      "MSZoning_C (all): 0.00037043471445244615\n",
      "ScreenPorch: 0.0003498880888278235\n",
      "BldgType_1Fam: 0.00034144098548515874\n",
      "LotConfig_Inside: 0.0003318877642095783\n",
      "Neighborhood_StoneBr: 0.00033170068978879573\n",
      "LandContour_Bnk: 0.0003285913013942508\n",
      "Neighborhood_OldTown: 0.000326323156268288\n",
      "MasVnrType_BrkFace: 0.0003234239968232035\n",
      "RoofStyle_Hip: 0.0003231474743026597\n",
      "GarageCond: 0.00031592288723246393\n",
      "HouseStyle_2Story: 0.0002901015000776216\n",
      "LotShape_Reg: 0.0002897455733674214\n",
      "LotShape_IR1: 0.00028291135524271556\n",
      "HouseStyle_1Story: 0.0002807753490469225\n",
      "LandSlope: 0.0002757573830081341\n",
      "Neighborhood_Edwards: 0.0002736086066955116\n",
      "Fence: 0.0002661924301151623\n",
      "Functional_Typ: 0.00026516926756525906\n",
      "Exterior1st+Exterior2nd_BrkFace+Wd Sdng: 0.0002513022250064297\n",
      "MSSubClass_160: 0.00023744183815948463\n",
      "Neighborhood_CollgCr: 0.00023616740578730671\n",
      "Neighborhood_Somerst: 0.0002331133818546954\n",
      "SaleCondition_Abnorml: 0.00023246959179835185\n",
      "Exterior1st+Exterior2nd_CemntBd+CmentBd: 0.00022777246156879238\n",
      "Neighborhood_NAmes: 0.00021800992427755485\n",
      "MasVnrType_None: 0.00019637658570155917\n",
      "Foundation_CBlock: 0.000192187379760717\n",
      "PavedDrive_Y: 0.00019212503410396533\n",
      "Exterior1st+Exterior2nd_Plywood+Plywood: 0.00018430738370852164\n",
      "BsmtFinSF2: 0.00018416240664781455\n",
      "Neighborhood_IDOTRR: 0.00017459039547456016\n",
      "Exterior1st+Exterior2nd_Wd Sdng+Wd Sdng: 0.00017312807092033898\n",
      "LotConfig_Corner: 0.0001727403622881246\n",
      "Functional_Mod: 0.00017266623215744917\n",
      "3SsnPorch: 0.00016805224671350935\n",
      "Neighborhood_NridgHt: 0.00016607523261565478\n",
      "Exterior1st+Exterior2nd_BrkComm+Brk Cmn: 0.00016448662808482808\n",
      "LotConfig_CulDSac: 0.00015922333610420342\n",
      "Foundation_BrkTil: 0.0001571282534274585\n",
      "Electrical_SBrkr: 0.000150489267274917\n",
      "Foundation_PConc: 0.00014954224315643993\n",
      "HouseStyle_1.5Fin: 0.0001480621514075348\n",
      "Fence_isExist: 0.00014070251069338464\n",
      "BsmtFinType2: 0.00014026264706071671\n",
      "PavedDrive_P: 0.0001332280900326594\n",
      "MSSubClass_50: 0.0001301844810728418\n",
      "Neighborhood_ClearCr: 0.00013002143311614432\n",
      "PavedDrive_N: 0.00011625373660833956\n",
      "Neighborhood_BrkSide: 0.00011121784244875033\n",
      "HouseStyle_2.5Fin: 0.000106804520398569\n",
      "Neighborhood_Gilbert: 0.00010082096218959228\n",
      "Condition1+Condition2_Norm+Norm: 0.00010043146428128196\n",
      "Alley_Pave: 9.8493609274051e-05\n",
      "MSSubClass_70: 9.679592070129382e-05\n",
      "Alley_Grvl: 9.185201266044012e-05\n",
      "Electrical_FuseA: 9.061483716118245e-05\n",
      "Alley_None: 9.054976125037453e-05\n",
      "Neighborhood_NWAmes: 8.839545510486689e-05\n",
      "MiscVal: 8.393212399989323e-05\n",
      "BsmtHalfBath: 8.136213836590333e-05\n",
      "Exterior1st+Exterior2nd_HdBoard+HdBoard: 8.075742591234318e-05\n",
      "HouseStyle_SLvl: 8.036457920175723e-05\n",
      "MSZoning_FV: 7.566514979907478e-05\n",
      "Neighborhood_Timber: 7.564180708624747e-05\n",
      "Neighborhood_SWISU: 7.331994164534054e-05\n",
      "Alley_isExist: 6.92411695402018e-05\n",
      "BldgType_TwnhsE: 6.34017665010745e-05\n",
      "LowQualFinSF: 6.137804625957673e-05\n",
      "Neighborhood_SawyerW: 5.8785614761110386e-05\n",
      "Condition1+Condition2_Feedr+Norm: 5.826090859459471e-05\n",
      "MSSubClass_120: 5.467686756254184e-05\n",
      "Functional_Min2: 5.442753213774841e-05\n",
      "LotConfig_FR2: 5.3618971172780454e-05\n",
      "Condition1+Condition2_Artery+Norm: 5.277500878538978e-05\n",
      "Neighborhood_NoRidge: 4.931679255934147e-05\n",
      "Heating_GasA: 4.775245922263671e-05\n",
      "Functional_Maj2: 4.583104008981362e-05\n",
      "SaleType_COD: 4.5223590155012475e-05\n",
      "MSSubClass_80: 4.2979364376998724e-05\n",
      "MiscFeature_Shed: 4.184445990236837e-05\n",
      "Neighborhood_Sawyer: 4.172950994649312e-05\n",
      "Exterior1st+Exterior2nd_Wd Sdng+VinylSd: 3.851394325215667e-05\n",
      "BldgType_Twnhs: 3.7824940263439526e-05\n",
      "Exterior1st+Exterior2nd_WdShing+Wd Shng: 3.7674532603369874e-05\n",
      "MiscFeature_None: 3.763142490187785e-05\n",
      "Neighborhood_Mitchel: 3.636317894681102e-05\n",
      "RoofStyle_Gambrel: 3.626778062229027e-05\n",
      "Exterior1st+Exterior2nd_BrkFace+BrkFace: 3.3840603736876086e-05\n",
      "MiscFeature_isExist: 3.380220428962665e-05\n",
      "Heating_GasW: 3.225418110079719e-05\n",
      "LotShape_IR2: 3.121119886058858e-05\n",
      "BldgType_Duplex: 2.8615063878225066e-05\n",
      "GarageFinish_None: 2.8609985916749845e-05\n",
      "MSSubClass_90: 2.8257719824448207e-05\n",
      "Functional_Maj1: 2.816230052891083e-05\n",
      "Exterior1st+Exterior2nd_Stucco+Stucco: 2.7171068575447077e-05\n",
      "MSSubClass_190: 2.6612400715799798e-05\n",
      "GarageType_None: 2.6301322124315995e-05\n",
      "Neighborhood_MeadowV: 2.6134057670396552e-05\n",
      "MSZoning_RH: 2.5352286352782934e-05\n",
      "Neighborhood_Veenker: 2.469830750453542e-05\n",
      "Garage_isExist: 2.2480025636446594e-05\n",
      "MSSubClass_40: 2.1515082128918944e-05\n",
      "Functional_Min1: 1.9081225386368114e-05\n",
      "Condition1+Condition2_PosN+Norm: 1.894393206813849e-05\n",
      "Condition1+Condition2_RRAn+Feedr: 1.8777171007564788e-05\n",
      "Bsmt_isExist: 1.819072115623829e-05\n",
      "Exterior1st+Exterior2nd_Stone+Stone: 1.7894507675125956e-05\n",
      "Heating_Grav: 1.72630609875486e-05\n",
      "Electrical_FuseF: 1.6886533950620017e-05\n",
      "Neighborhood_NPkVill: 1.6707728053197873e-05\n",
      "Exterior1st+Exterior2nd_Stucco+CmentBd: 1.6067403787622944e-05\n",
      "Neighborhood_BrDale: 1.5198818913054068e-05\n",
      "BldgType_2fmCon: 1.4423931225509111e-05\n",
      "SaleType_CWD: 1.3315107995040684e-05\n",
      "PoolQC: 1.3010001030478151e-05\n",
      "Foundation_Slab: 1.2633144632245497e-05\n",
      "GarageType_CarPort: 1.2626326799636808e-05\n",
      "HouseStyle_SFoyer: 1.204897952765236e-05\n",
      "Pool_isExist: 1.1867751997383018e-05\n",
      "Exterior1st+Exterior2nd_BrkFace+Stone: 1.1749036642620294e-05\n",
      "Exterior1st+Exterior2nd_AsbShng+AsbShng: 1.1642268465837e-05\n",
      "SaleType_Con: 1.108838348226644e-05\n",
      "MSSubClass_75: 1.0333109294364155e-05\n",
      "Exterior1st+Exterior2nd_Plywood+Brk Cmn: 1.0220285428772259e-05\n",
      "RoofMatl_WdShngl: 9.734095951360532e-06\n",
      "LandContour_Low: 9.672652703443843e-06\n",
      "HouseStyle_2.5Unf: 9.538878228143582e-06\n",
      "MasVnrType_BrkCmn: 9.248876479203826e-06\n",
      "SaleType_ConLw: 8.782892914357936e-06\n",
      "SaleType_ConLI: 8.462214261976803e-06\n",
      "PoolArea: 8.323253077490559e-06\n",
      "HouseStyle_1.5Unf: 8.137914936073856e-06\n",
      "Exterior1st+Exterior2nd_AsphShn+AsphShn: 8.095451156715705e-06\n",
      "Exterior1st+Exterior2nd_VinylSd+Other: 8.038708600185214e-06\n",
      "Exterior1st+Exterior2nd_MetalSd+Wd Sdng: 7.756136883184304e-06\n",
      "Foundation_Stone: 7.654502172947851e-06\n",
      "Neighborhood_Blmngtn: 7.4774651691117585e-06\n",
      "GarageType_2Types: 7.282116701641864e-06\n",
      "MSSubClass_45: 7.0514391343559874e-06\n",
      "RoofMatl_CompShg: 6.919149927838334e-06\n",
      "Exterior1st+Exterior2nd_VinylSd+Wd Shng: 6.371286522806168e-06\n",
      "LotConfig_FR3: 6.060656855600951e-06\n",
      "SaleType_Oth: 5.704872291602773e-06\n",
      "Condition1+Condition2_RRAe+Norm: 5.6354447272040685e-06\n",
      "Condition1+Condition2_RRAn+Norm: 5.533062923226466e-06\n",
      "RoofStyle_Mansard: 4.40768591407792e-06\n",
      "GarageType_Basment: 4.192439151704522e-06\n",
      "MSSubClass_85: 3.944563636740069e-06\n",
      "Exterior1st+Exterior2nd_Wd Sdng+Wd Shng: 3.68006463913295e-06\n",
      "Electrical_Mix: 3.554576542513762e-06\n",
      "Exterior1st+Exterior2nd_Plywood+Wd Sdng: 3.425832569914402e-06\n",
      "Exterior1st+Exterior2nd_HdBoard+Plywood: 3.268268171188754e-06\n",
      "MasVnr_isExist: 2.823297775056403e-06\n",
      "Exterior1st+Exterior2nd_MetalSd+Stucco: 2.7055080592336997e-06\n",
      "Exterior1st+Exterior2nd_MetalSd+AsphShn: 2.4505037311852937e-06\n",
      "Exterior1st+Exterior2nd_AsbShng+Stucco: 2.414095383873551e-06\n",
      "SaleCondition_Alloca: 2.4084811063180886e-06\n",
      "Exterior1st+Exterior2nd_VinylSd+ImStucc: 2.378033494662623e-06\n",
      "SaleCondition_AdjLand: 2.3248600013498094e-06\n",
      "Exterior1st+Exterior2nd_Wd Sdng+Plywood: 2.2973721495279818e-06\n",
      "SaleType_ConLD: 2.211542783742821e-06\n",
      "Exterior1st+Exterior2nd_BrkFace+Wd Shng: 1.9698549391991947e-06\n",
      "Exterior1st+Exterior2nd_Wd Sdng+BrkFace: 1.9596611254495487e-06\n",
      "Exterior1st+Exterior2nd_ImStucc+ImStucc: 1.9218682827382973e-06\n",
      "Exterior1st+Exterior2nd_WdShing+Plywood: 1.8762978886887332e-06\n",
      "Exterior1st+Exterior2nd_Wd Sdng+AsbShng: 1.6563626290065537e-06\n",
      "Electrical_FuseP: 1.5782732834126613e-06\n",
      "MiscFeature_Othr: 1.4782460516594617e-06\n",
      "Exterior1st+Exterior2nd_MetalSd+HdBoard: 1.3701280844010606e-06\n",
      "Foundation_Wood: 1.3430448998999334e-06\n",
      "Exterior1st+Exterior2nd_AsbShng+Plywood: 1.3177233703350722e-06\n",
      "Exterior1st+Exterior2nd_BrkFace+HdBoard: 1.2648646350799778e-06\n",
      "Condition1+Condition2_Feedr+RRNn: 1.2371723902654898e-06\n",
      "Exterior1st+Exterior2nd_VinylSd+AsbShng: 1.2206525875279735e-06\n",
      "MSSubClass_180: 1.0776259142480888e-06\n",
      "RoofMatl_WdShake: 1.0699730085995118e-06\n",
      "Exterior1st+Exterior2nd_Plywood+ImStucc: 9.186420295580441e-07\n",
      "Heating_Wall: 8.965458447783492e-07\n",
      "Exterior1st+Exterior2nd_HdBoard+ImStucc: 8.781170820135185e-07\n",
      "Neighborhood_Blueste: 7.12944768358542e-07\n",
      "RoofStyle_Flat: 6.424002667960264e-07\n",
      "Exterior1st+Exterior2nd_WdShing+HdBoard: 6.236308377886105e-07\n",
      "Condition1+Condition2_PosA+Norm: 6.143216980103695e-07\n",
      "Exterior1st+Exterior2nd_VinylSd+Plywood: 5.892961998996476e-07\n",
      "Exterior1st+Exterior2nd_Stucco+Stone: 5.528020658470624e-07\n",
      "Street_Grvl: 4.803096969873752e-07\n",
      "Condition1+Condition2_RRNn+Feedr: 3.265047033956633e-07\n",
      "Exterior1st+Exterior2nd_HdBoard+AsphShn: 3.014901218241578e-07\n",
      "RoofMatl_Roll: 2.504760880553436e-07\n",
      "Exterior1st+Exterior2nd_BrkFace+Plywood: 2.1993454719985082e-07\n",
      "Street_Pave: 1.7919362810058618e-07\n",
      "Condition1+Condition2_Artery+PosA: 1.57053253860301e-07\n",
      "Exterior1st+Exterior2nd_VinylSd+Wd Sdng: 1.4060821180756874e-07\n",
      "Condition1+Condition2_Artery+Artery: 1.0048164012138337e-07\n",
      "Exterior1st+Exterior2nd_CBlock+CBlock: 8.209293902639503e-08\n",
      "RoofMatl_Tar&Grv: 4.903350702905e-08\n",
      "Condition1+Condition2_RRNn+Norm: 1.2381698768056283e-08\n",
      "Exterior1st+Exterior2nd_MetalSd+Wd Shng: 7.920958396216195e-09\n",
      "Utilities: 0.0\n",
      "LotShape_IR3: 0.0\n",
      "Heating_OthW: 0.0\n",
      "Exterior1st+Exterior2nd_WdShing+Wd Sdng: 0.0\n",
      "Exterior1st+Exterior2nd_WdShing+Stucco: 0.0\n",
      "Exterior1st+Exterior2nd_Wd Sdng+MetalSd: 0.0\n",
      "Exterior1st+Exterior2nd_Wd Sdng+ImStucc: 0.0\n",
      "Exterior1st+Exterior2nd_VinylSd+Stucco: 0.0\n",
      "Exterior1st+Exterior2nd_VinylSd+HdBoard: 0.0\n",
      "Exterior1st+Exterior2nd_Stucco+Wd Shng: 0.0\n",
      "Exterior1st+Exterior2nd_Plywood+HdBoard: 0.0\n",
      "Exterior1st+Exterior2nd_HdBoard+Wd Shng: 0.0\n",
      "Exterior1st+Exterior2nd_HdBoard+MetalSd: 0.0\n",
      "Condition1+Condition2_RRNe+Norm: 0.0\n"
     ]
    }
   ],
   "source": [
    "features_importances = sorted(zip(rand_forest.feature_importances_, X_train.columns), reverse=True)\n",
    "for val, name in features_importances:\n",
    "    print(f'{name}: {val}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Попробуем построить модель линейной регрессии на всех полученных признаках"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean r2-score: -6.166590489813555e+25\n",
      "\n",
      "1-fold r2-score: -6.27856794951858e+24\n",
      "2-fold r2-score: -1.2183292500650867e+22\n",
      "3-fold r2-score: -3.5935742804489997e+25\n",
      "4-fold r2-score: -4.410740434357914e+22\n",
      "5-fold r2-score: -1.931076724149456e+24\n",
      "6-fold r2-score: -4.9742285003671584e+26\n",
      "7-fold r2-score: -7.312272986416438e+25\n",
      "8-fold r2-score: -5.371137347017252e+19\n",
      "9-fold r2-score: -6.207150905452648e+19\n",
      "10-fold r2-score: -1.9116751225904963e+24\n"
     ]
    }
   ],
   "source": [
    "lr = LinearRegression()\n",
    "\n",
    "scaler = StandardScaler()     \n",
    "scaler.fit(X_train)\n",
    "X_train_std = scaler.transform(X_train)\n",
    "X_test_std = scaler.transform(X_test)\n",
    "\n",
    "cv_result = cross_val_score(lr, X=X_train_std, y=y_train, scoring='r2', cv=10)\n",
    "print(f'mean r2-score: {cv_result.mean()}\\n')\n",
    "for cv, val in enumerate(cv_result, 1):\n",
    "    print(f'{cv}-fold r2-score: {val}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Получили отрицательное значение, значит ошибка нашей модели значительно больше ошибок модели методом усреднения.\n",
    "Проверим соотношение количества признаков и количества обучающих образцов."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Количество признаков в модели: 277\n",
      "Количество обучающих образцов в модели: 1067\n"
     ]
    }
   ],
   "source": [
    "print(f'Количество признаков в модели: {len(df_house.columns)}')\n",
    "print(f'Количество обучающих образцов в модели: {len(df_house)}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Количество признаков составляет почти четверть количества образцов. \n",
    "Попробуем сжать признаки с помощью PCA. Вначале подберем наилушее значение главных компонент."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Наилучший результат: mean r2-score 0.86326, количество компонент 56\n"
     ]
    }
   ],
   "source": [
    "pca_result = []\n",
    "for n_components in range(10, len(df_house.columns)):\n",
    "    pca = PCA(n_components=n_components, random_state=42)\n",
    "    pca.fit(X_train_std)\n",
    "    X_train_std_pca = pca.transform(X_train_std)\n",
    "    X_test_std_pca = pca.transform(X_test_std)\n",
    "    cv_result = cross_val_score(lr, X=X_train_std_pca, y=y_train, scoring='r2', cv=10)\n",
    "    pca_result.append((cv_result.mean(), n_components))\n",
    "    \n",
    "print(f'Наилучший результат: mean r2-score {round(max(pca_result)[0], 6)}, ' \n",
    "      f'количество компонент {max(pca_result)[1]}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Теперь найдем статистику по фолдам для нашего наилучшего количества главных компонент"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean r2-score: 0.8632598996315458\n",
      "\n",
      "1-fold r2-score: 0.9108647060325944\n",
      "2-fold r2-score: 0.8968720105501269\n",
      "3-fold r2-score: 0.8891115745516083\n",
      "4-fold r2-score: 0.853002755521647\n",
      "5-fold r2-score: 0.9011793390780989\n",
      "6-fold r2-score: 0.8754410450673764\n",
      "7-fold r2-score: 0.8182233860778021\n",
      "8-fold r2-score: 0.7753176216451823\n",
      "9-fold r2-score: 0.8524541346313079\n",
      "10-fold r2-score: 0.8601324231597138\n"
     ]
    }
   ],
   "source": [
    "pca = PCA(n_components=56, random_state=42)\n",
    "pca.fit(X_train_std)\n",
    "X_train_std_pca = pca.transform(X_train_std)\n",
    "X_test_std_pca = pca.transform(X_test_std)\n",
    "\n",
    "cv_result = cross_val_score(lr, X=X_train_std_pca, y=y_train, scoring='r2', cv=10)\n",
    "print(f'mean r2-score: {cv_result.mean()}\\n')\n",
    "for cv, val in enumerate(cv_result, 1):\n",
    "    print(f'{cv}-fold r2-score: {val}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Средний результат значительно улучшился и перестал быть отрицательным. В некоторых разделениях даже удается получить оценку более 0.9.\n",
    "\n",
    "Теперь попробуем обучиться только на тех признаках, которые считаются важными для леса случайных деревьев."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean r2-score: 0.885644052405483\n",
      "\n",
      "1-fold r2-score: 0.9266697315904395\n",
      "2-fold r2-score: 0.8873910672384412\n",
      "3-fold r2-score: 0.9021009480259959\n",
      "4-fold r2-score: 0.9085217077977701\n",
      "5-fold r2-score: 0.8937829785034481\n",
      "6-fold r2-score: 0.9241793577327966\n",
      "7-fold r2-score: 0.8451019735009213\n",
      "8-fold r2-score: 0.8220324276006588\n",
      "9-fold r2-score: 0.8597453242574695\n",
      "10-fold r2-score: 0.8869150078068887\n"
     ]
    }
   ],
   "source": [
    "filtered_features = [name for val, name in features_importances if val > 0.001]\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(df_house[filtered_features],\n",
    "                                                    df_house['SalePrice'],\n",
    "                                                    test_size=0.3,\n",
    "                                                    random_state=42)\n",
    "\n",
    "scaler = StandardScaler()     \n",
    "scaler.fit(X_train)\n",
    "X_train_std = scaler.transform(X_train)\n",
    "X_test_std = scaler.transform(X_test)\n",
    "\n",
    "cv_result = cross_val_score(lr, X=X_train_std, y=y_train, scoring='r2', cv=10)\n",
    "print(f'mean r2-score: {cv_result.mean()}\\n')\n",
    "for cv, val in enumerate(cv_result, 1):\n",
    "    print(f'{cv}-fold r2-score: {val}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "С помощью удаления ненужных признаков мы смогли немного улучшить качество нашей модели по сравнению со сжатием PCA. Теперь попробуем создать модель на основе стекинга."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mean r2-score: 0.901785728756803\n",
      "\n",
      "1-fold r2-score: 0.9496110651931521\n",
      "2-fold r2-score: 0.9304356768535316\n",
      "3-fold r2-score: 0.9085896210634767\n",
      "4-fold r2-score: 0.9003235266816371\n",
      "5-fold r2-score: 0.9179892359258458\n",
      "6-fold r2-score: 0.9373777074102527\n",
      "7-fold r2-score: 0.8612563162445824\n",
      "8-fold r2-score: 0.8167612454298405\n",
      "9-fold r2-score: 0.8741226104631739\n",
      "10-fold r2-score: 0.9213902823025362\n"
     ]
    }
   ],
   "source": [
    "stack_estimators = [('svr', SVR()),\n",
    "                    ('lr', LinearRegression()),\n",
    "                    ('tree_reg', DecisionTreeRegressor(random_state=42)),\n",
    "                    ('rf_reg', RandomForestRegressor(n_estimators=100, random_state=42)),\n",
    "                   ]\n",
    "\n",
    "stack_meta_estimator = LinearRegression()\n",
    "\n",
    "stack = StackingRegressor(estimators=stack_estimators, final_estimator=stack_meta_estimator, cv=10)\n",
    "\n",
    "cv_result = cross_val_score(stack, X=X_train_std, y=y_train, scoring='r2', cv=10)\n",
    "print(f'mean r2-score: {cv_result.mean()}\\n')\n",
    "for cv, val in enumerate(cv_result, 1):\n",
    "    print(f'{cv}-fold r2-score: {val}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Удалось улучшить результат по сравнению с обычной линейной регрессией. Теперь убедимся, что модель второго уровня действительно улучшает результаты моделей первого уровня."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "StackingRegressor(cv=10,\n",
       "                  estimators=[('svr', SVR()), ('lr', LinearRegression()),\n",
       "                              ('tree_reg',\n",
       "                               DecisionTreeRegressor(random_state=42)),\n",
       "                              ('rf_reg',\n",
       "                               RandomForestRegressor(random_state=42))],\n",
       "                  final_estimator=LinearRegression())"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stack.fit(X_train_std, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "svr mean r2-score: -0.09204335980098635\n",
      "\n",
      "lr mean r2-score: 0.885644052405483\n",
      "\n",
      "tree_reg mean r2-score: 0.7554666970760503\n",
      "\n",
      "rf_reg mean r2-score: 0.883022711402903\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for name, estimator in stack.named_estimators_.items():\n",
    "    cv_result = cross_val_score(estimator, X=X_train_std, y=y_train, scoring='r2', cv=10)\n",
    "    print(f'{name} mean r2-score: {cv_result.mean()}\\n')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Итоговый средний результат R2 для стекинга: 0.9017. Этот результат превышает все средние оценки моделей первого уровня. Отсюда делаем заключение, что обобщающая модель второго уровня дает лучшие результаты, чем отдельно взятые модели первого уровня."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
